﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using SCH_Tools.Modules;

namespace ReaXML
{
    public class XmlGenerate
    {
        string sFile = @"plugins\sch-tools\sch-tools.xml";
        public void Initialeze()
        {
            if (!Directory.Exists(@"plugins\sch-tools"))
            {
                Directory.CreateDirectory(@"plugins\sch-tools");
            }

            if (!File.Exists(sFile))
            {
                FileStream fs = new FileStream(sFile, FileMode.OpenOrCreate);
                XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8);
                w.Indentation = 4;
                w.Formatting = Formatting.Indented;

                w.WriteStartDocument();
                w.WriteStartElement("configuration");

                w.WriteStartElement("TypeServer");

                w.WriteStartElement("Server");
                w.WriteAttributeString("Name", "SQLSERVER");
                w.WriteElementString("CMD", "sqlcmd");
                w.WriteElementString("Driver", "{SQL Server}");
                w.WriteElementString("Path", string.Empty);
                w.WriteElementString("ParamHost", "H");
                w.WriteElementString("ParamServer", "S");
                w.WriteElementString("ParamUser", "U");
                w.WriteElementString("ParamPass", "P");
                w.WriteElementString("ParamInput", "i");
                w.WriteElementString("ParamOutput", "o");
                w.WriteEndElement();

                w.WriteStartElement("Server");
                w.WriteAttributeString("Name", "SYBASE");
                w.WriteElementString("CMD", "isql");
                w.WriteElementString("Driver", "{SYBASE ASE ODBC Driver}");
                w.WriteElementString("Path", string.Empty);
                w.WriteElementString("ParamHost", string.Empty);
                w.WriteElementString("ParamServer", string.Empty);
                w.WriteElementString("ParamUser", string.Empty);
                w.WriteElementString("ParamPass", string.Empty);
                w.WriteElementString("ParamInput", string.Empty);
                w.WriteElementString("ParamOutput", string.Empty);
                w.WriteEndElement();

                w.WriteStartElement("Server");
                w.WriteAttributeString("Name", "MYSQL");
                w.WriteElementString("CMD", "mysql");
                w.WriteElementString("Driver", "{MySQL ODBC 3.51 Driver}");
                w.WriteElementString("Path", string.Empty);
                w.WriteElementString("ParamHost", "h");
                w.WriteElementString("ParamServer", "h");
                w.WriteElementString("ParamUser", "u");
                w.WriteElementString("ParamPass", "p");
                w.WriteElementString("ParamInput", "e");
                w.WriteElementString("ParamOutput", "X");
                w.WriteEndElement();

                w.WriteStartElement("Server");
                w.WriteAttributeString("Name", "ISQL");
                w.WriteElementString("CMD", "isql");
                w.WriteElementString("Driver", string.Empty);
                w.WriteElementString("Path", string.Empty);
                w.WriteElementString("ParamHost", "H");
                w.WriteElementString("ParamServer", "S");
                w.WriteElementString("ParamUser", "U");
                w.WriteElementString("ParamPass", "P");
                w.WriteElementString("ParamInput", "i");
                w.WriteElementString("ParamOutput", "o");
                w.WriteEndElement();

                w.WriteStartElement("Server");
                w.WriteAttributeString("Name", "OSQL");
                w.WriteElementString("CMD", "osql");
                w.WriteElementString("Driver", string.Empty);
                w.WriteElementString("Path", string.Empty);
                w.WriteElementString("ParamHost", "H");
                w.WriteElementString("ParamServer", "S");
                w.WriteElementString("ParamUser", "U");
                w.WriteElementString("ParamPass", "P");
                w.WriteElementString("ParamInput", "i");
                w.WriteElementString("ParamOutput", "o");
                w.WriteEndElement();

                w.WriteEndElement();

                w.WriteStartElement("Servers");
                w.WriteEndElement();
                w.WriteEndElement();
                w.Flush();
                fs.Close();
            }
        }

        public List<Server> GetListServer()
        {
            return GetInformationServer("");
        }

        public void RemoveServer(Server obj)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(sFile);

            XmlNodeList elementNodeList = doc.SelectNodes("/configuration/Servers");
            if (elementNodeList.Count > 0)
            {
                elementNodeList[0].RemoveChild(doc.SelectSingleNode("/configuration/Servers/Server[@name='" + obj.ServerName + "']"));
            }
            doc.Save(sFile);
        }

        public void ServerMaintenance(Server obj)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(sFile);

            XmlNodeList elementNodeList = doc.SelectNodes("/configuration/Servers/Server[@name='" + obj.ServerName + "']");
            if (elementNodeList.Count == 0)
            {
                XmlNode parent = doc.DocumentElement.SelectSingleNode("/configuration/Servers");
                XmlNode node = doc.CreateNode(XmlNodeType.Element, "Server", null);
                XmlAttribute attr = doc.CreateAttribute("name");
                attr.Value = obj.ServerName;

                node.Attributes.Append(attr);

                XmlNode nodeHost = doc.CreateElement("Host");
                nodeHost.InnerText = obj.Host;

                XmlNode nodeAlias = doc.CreateElement("Alias");
                nodeAlias.InnerText = obj.Alias;

                XmlNode nodeIp = doc.CreateElement("ip");
                nodeIp.InnerText = obj.Ip;

                XmlNode nodePort = doc.CreateElement("port");
                nodePort.InnerText = obj.Port;

                XmlNode nodeUser = doc.CreateElement("user");
                nodeUser.InnerText = obj.User;

                XmlNode nodePass = doc.CreateElement("pass");
                nodePass.InnerText = obj.Pass;

                XmlNode nodeType = doc.CreateElement("type");
                nodeType.InnerText = obj.ServerType;
                node.AppendChild(nodeHost);
                node.AppendChild(nodeAlias);
                node.AppendChild(nodeIp);
                node.AppendChild(nodePort);
                node.AppendChild(nodeUser);
                node.AppendChild(nodePass);
                node.AppendChild(nodeType);
                parent.AppendChild(node);
                doc.DocumentElement.AppendChild(parent);
            }
            else
            {
                XmlNodeList elemHost = doc.SelectNodes("/configuration/Servers/Server[@name='" + obj.ServerName + "']/Host");
                elemHost[0].InnerText = obj.Host;
                XmlNodeList elemAlias = doc.SelectNodes("/configuration/Servers/Server[@name='" + obj.ServerName + "']/Alias");
                elemAlias[0].InnerText = obj.Alias;
                XmlNodeList elemIp = doc.SelectNodes("/configuration/Servers/Server[@name='" + obj.ServerName + "']/ip");
                elemIp[0].InnerText = obj.Ip;
                XmlNodeList elemPort = doc.SelectNodes("/configuration/Servers/Server[@name='" + obj.ServerName + "']/port");
                elemPort[0].InnerText = obj.Port;
                XmlNodeList elemUser = doc.SelectNodes("/configuration/Servers/Server[@name='" + obj.ServerName + "']/user");
                elemUser[0].InnerText = obj.User;
                XmlNodeList elemPass = doc.SelectNodes("/configuration/Servers/Server[@name='" + obj.ServerName + "']/pass");
                elemPass[0].InnerText = obj.Pass;
                XmlNodeList elemType = doc.SelectNodes("/configuration/Servers/Server[@name='" + obj.ServerName + "']/type");
                elemType[0].InnerText = obj.ServerType;
            }
            //save back
            doc.Save(sFile);
        }

        public void ServerCmdMaintenance(ServerDriver obj)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(sFile);

            XmlNode elementRemove = doc.SelectSingleNode("/configuration/TypeServer/Server[@Name='" + obj.Name + "']");
            elementRemove.ParentNode.RemoveChild(elementRemove);

            XmlNodeList elementNodeList = doc.SelectNodes("/configuration/TypeServer/Server[@Name='" + obj.Name + "']");
            if (elementNodeList.Count == 0)
            {
                XmlNode parent = doc.DocumentElement.SelectSingleNode("/configuration/TypeServer");
                XmlNode node = doc.CreateNode(XmlNodeType.Element, "Server", null);
                XmlAttribute attr = doc.CreateAttribute("Name");
                attr.Value = obj.Name;

                node.Attributes.Append(attr);

                XmlNode nodeCmd = doc.CreateElement("CMD");
                nodeCmd.InnerText = obj.CmdName;

                XmlNode nodeDriver = doc.CreateElement("Driver");
                nodeDriver.InnerText = obj.Driver;

                XmlNode nodePath = doc.CreateElement("Path");
                nodePath.InnerText = obj.Path;

                XmlNode nodeParamHost = doc.CreateElement("ParamHost");
                nodeParamHost.InnerText = obj.ParamHost;

                XmlNode nodeParamServer = doc.CreateElement("ParamServer");
                nodeParamServer.InnerText = obj.ParamServer;

                XmlNode nodeParamUser = doc.CreateElement("ParamUser");
                nodeParamUser.InnerText = obj.ParamUser;

                XmlNode nodeParamPass = doc.CreateElement("ParamPass");
                nodeParamPass.InnerText = obj.ParamPass;

                XmlNode nodeParamInput = doc.CreateElement("ParamInput");
                nodeParamInput.InnerText = obj.ParamInput;

                XmlNode nodeParamOutput = doc.CreateElement("ParamOutput");
                nodeParamOutput.InnerText = obj.ParamOutput;

                node.AppendChild(nodeCmd);
                node.AppendChild(nodeDriver);
                node.AppendChild(nodePath);
                node.AppendChild(nodeParamHost);
                node.AppendChild(nodeParamServer);
                node.AppendChild(nodeParamUser);
                node.AppendChild(nodeParamPass);
                node.AppendChild(nodeParamInput);
                node.AppendChild(nodeParamOutput);

                parent.AppendChild(node);
                doc.DocumentElement.AppendChild(parent);
            }
            //save back
            doc.Save(sFile);
        }

        public List<ServerDriver> GetListServerDriver(string ServerDriver)
        {
            List<ServerDriver> CollectionServer = new List<ServerDriver>();
            string findstr = @"/configuration/TypeServer/Server";
            if (!string.IsNullOrEmpty(ServerDriver))
            {
                findstr += @"[@Name='" + ServerDriver + "']";
            }

            XmlDocument doc = new XmlDocument();
            doc.Load(sFile);
            XmlElement root = doc.DocumentElement;
            XmlNodeList nodes = root.SelectNodes(findstr);

            foreach (XmlNode itemnode in nodes)
            {
                ServerDriver ItemServer = new ServerDriver();

                ItemServer.Name = itemnode.Attributes["Name"].Value;
                foreach (XmlElement element in itemnode)
                {
                    switch (element.Name)
                    {
                        case "CMD":
                            ItemServer.CmdName = element.InnerText;
                            break;
                        case "Driver":
                            ItemServer.Driver = element.InnerText;
                            break;
                        case "Path":
                            ItemServer.Path = element.InnerText;
                            break;
                        case "ParamHost":
                            ItemServer.ParamHost = element.InnerText;
                            break;
                        case "ParamServer":
                            ItemServer.ParamServer = element.InnerText;
                            break;
                        case "ParamUser":
                            ItemServer.ParamUser = element.InnerText;
                            break;
                        case "ParamPass":
                            ItemServer.ParamPass = element.InnerText;
                            break;
                        case "ParamInput":
                            ItemServer.ParamInput = element.InnerText;
                            break;
                        case "ParamOutput":
                            ItemServer.ParamOutput = element.InnerText;
                            break;
                    }
                }
                CollectionServer.Add(ItemServer);
            }
            return CollectionServer;
        }

        public List<Server> GetInformationServer(string server)
        {
            List<Server> ListServer = new List<Server>();
            string findstr = @"/configuration/Servers/Server";
            if (!string.IsNullOrEmpty(server))
            {
                findstr += @"[@name='" + server + "']";
            }

            XmlDocument doc = new XmlDocument();
            doc.Load(sFile);
            System.Xml.XmlNodeList nodes = doc.DocumentElement.SelectNodes(findstr);

            foreach (XmlNode itemnode in nodes)
            {
                Server ItemServer = new Server();

                ItemServer.ServerName = itemnode.Attributes["name"].Value;

                foreach (XmlElement element in itemnode)
                {
                    switch (element.Name)
                    {
                        case "Host":
                            ItemServer.Host = element.InnerText;
                            break;
                        case "Alias":
                            ItemServer.Alias = element.InnerText;
                            break;
                        case "ip":
                            ItemServer.Ip = element.InnerText;
                            break;
                        case "port":
                            ItemServer.Port = element.InnerText;
                            break;
                        case "user":
                            ItemServer.User = element.InnerText;
                            break;
                        case "pass":
                            ItemServer.Pass = element.InnerText;
                            break;
                        case "type":
                            ItemServer.ServerType = element.InnerText;
                            break;
                    }
                }
                ListServer.Add(ItemServer);
            }
            return ListServer;
        }

    }
}
